# -*- coding: utf-8 -*-
"""
Created on Tue Apr 11 16:13:46 2023

@author: admin
"""

from os import chdir
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pylab as mpl
import matplotlib.patches as mpatches
from curlyBrace import curlyBrace

path = 'E:\\Teaching\\微积分\\ch1\\'
chdir(path)

###########
#Define the x and y ranges, and the tick interval for both axes.
xmin, xmax, ymin, ymax = 0.7, 1, 0.7, 1
ticks_frequency = 1
#Create a figure and an axes object. Also set the face color.
fig, ax = plt.subplots(figsize=(10, 10))
fig.patch.set_facecolor('#ffffff')
#Apply the ranges to the axes.
ax.set(xlim=(xmin-1, xmax+1), ylim=(ymin-1, ymax+1), aspect='equal')
#Set both axes to the zero position.
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
#Hide the top and right spines.
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
#Set the x and y labels, and add an origin label.
ax.set_xlabel('$x$', size=14, labelpad=-24, x=1.02)
ax.set_ylabel('$y$', size=14, labelpad=-21, y=1.02, rotation=0)
 
plt.text(-0.01, -0.01, r"$O$", ha='right', va='top',
         horizontalalignment='center', fontsize=18)
#create the x and the y ticks, and apply them to both axes.
x_ticks = np.arange(xmin, xmax+1, ticks_frequency)
y_ticks = np.arange(ymin, ymax+1, ticks_frequency)
#ax.set_xticks(x_ticks[x_ticks != 0])
#ax.set_yticks(y_ticks[y_ticks != 0])
#ax.set_xticks(np.arange(xmin, xmax+1), minor=True)
#ax.set_yticks([-1,1])
plt.tick_params(bottom = False,labelbottom = False,left=False,labelleft=False)
#add a grid.
#ax.grid(which='both', color='grey', linewidth=1, linestyle='-', alpha=0.2)
ax.plot(xmax+1, 0, ">k", clip_on=False)
ax.plot(0, ymax+1, "^k", clip_on=False)
kw1 = dict(ls='-.', color='r',linewidth=1.5)

#plot a function on it.
x1 = np.linspace(0.6,1.7, 50)
y1 = 0.5*x1**2
plt.plot(x1, y1, 'b', linewidth=2)
#####
x = np.linspace(0,0.6, 50)
y = [y1[0]]*len(x)
plt.plot(x, y, 'r--', linewidth=1.5)
y=np.linspace(0,y1[0],10)
x = [0.6]*len(y)
plt.plot(x, y, 'r--', linewidth=1.5)
x = np.linspace(0,1.7, 50)
y = [y1[-1]]*len(x)
plt.plot(x, y, 'r--', linewidth=1.5)
y=np.linspace(0,0.5*1.2*1.2,10)
x = [1.2]*len(y)
plt.plot(x, y, 'r--', linewidth=1.5)
x = np.linspace(0,1.2, 50)
y = [0.5*1.2*1.2]*len(x)
plt.plot(x, y, 'r--', linewidth=1.5)
y=np.linspace(0,y1[-1],10)
x = [1.7]*len(y)
plt.plot(x, y, 'r--', linewidth=1.5)
#####
plt.text(1.18,-0.05,r'$x$',fontsize=12)
plt.text(-0.05,0.5*1.2*1.2-0.02,r'$y$',fontsize=12)
plt.text(1.2,1,r'$y=f(x)$',fontsize=12)
plt.text(1.22,0.5*1.2*1.2-0.02,r'$(x,y)$',fontsize=12)
plt.text(1.12,-0.2,r'$D$',fontsize=12)
plt.text(-0.22,0.8,r'$R_{f}$',fontsize=12)
#####
kw2 = dict(k_r=0.04,bool_auto=True,color='k')
curlyBrace(fig,ax,(1.7,-0.05),(0.6,-0.05),**kw2) 
curlyBrace(fig,ax,(-0.05,0.5*0.6*0.6),(-0.05,0.5*1.7*1.7),**kw2) 
# Ask Matplotlib to show the plot
plt.savefig('fig-1.png',dpi=300)
plt.show()

